package util

import (
	"gitee.com/zaper/private-logger"
	"github.com/IBM/sarama"
	"github.com/segmentio/kafka-go"
)

type KafkaWRIo struct {
	Writer *kafka.Writer
	Reader *kafka.Reader
}

func CloseWRIo(kafkaWRIo *KafkaWRIo) {
	if kafkaWRIo.Writer != nil {
		err := kafkaWRIo.Writer.Close()
		if err != nil {
			logger.Info("Writer is close fail")
			return
		}
	}
	if kafkaWRIo.Reader != nil {
		err := kafkaWRIo.Reader.Close()
		if err != nil {
			logger.Info("Reader is close fail")
			return
		}
	}
}

type KafkaProType struct {
	SyncPro  sarama.SyncProducer
	AsyncPro sarama.AsyncProducer
}

type KafkaPCIo struct {
	Producer *KafkaProType
	Consumer sarama.Consumer
}

func ClosePC1Io(kafkaPCIo *KafkaPCIo) {
	if kafkaPCIo.Producer.SyncPro != nil {
		err := kafkaPCIo.Producer.SyncPro.Close()
		if err != nil {
			logger.Info("SyncProducer is close fail")
			return
		}
	}

	if kafkaPCIo.Producer.AsyncPro != nil {
		err := kafkaPCIo.Producer.AsyncPro.Close()
		if err != nil {
			logger.Info("AsyncProducer is close fail")
			return
		}
	}

	if kafkaPCIo.Consumer != nil {
		err := kafkaPCIo.Consumer.Close()
		if err != nil {
			logger.Info("Consumer is close fail")
			return
		}
	}
}
